package ps.client.gui.userlist;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.border.EmptyBorder;

import ps.server.net.ClientInfo;


@SuppressWarnings("serial")
public class UserListPanel extends JPanel {
    
    private static final String INFO_USER_CONNECTED = "Verbunden: ";
    
    JPanel contentPanel = new JPanel(new BorderLayout());
    JScrollPane userListScrollPane = new JScrollPane(contentPanel);
    
    UserListModel userListModel = new UserListModel() {
        protected void fireListDataChanged() {
            infoUserConnectedLabel.setText(INFO_USER_CONNECTED + userListModel.getSize());
        }
    };
    JList userList = new JList(userListModel);
    
    JPanel infoPanel = new JPanel();
    JLabel infoUserConnectedLabel = new JLabel(INFO_USER_CONNECTED + "0");
    
    JPopupMenu popupMenu = new JPopupMenu();
    JMenuItem group1Item = new JMenuItem("Gruppe 1");
    JMenuItem group2Item = new JMenuItem("Gruppe 2");
    JMenuItem group3Item = new JMenuItem("Gruppe 3");
    JMenuItem group4Item = new JMenuItem("Gruppe 4");
    JMenuItem noGroupItem = new JMenuItem("keine Gruppe");
    JMenuItem makeParseSharer = new JMenuItem("Parse-Sharer bestimmen");
    JMenuItem removeParseSharer = new JMenuItem("Parse-Sharer entfernen");
    
    boolean showPopup = false;
    
    public UserListPanel() {
        setLayout(new BorderLayout());
        add(userListScrollPane, BorderLayout.CENTER);
        add(infoPanel, BorderLayout.SOUTH);
        
        contentPanel.add(userList, BorderLayout.CENTER);
        
        userListScrollPane.setPreferredSize(new Dimension(170, 400));
        userListScrollPane.getVerticalScrollBar().setSize(new Dimension(15, Integer.MAX_VALUE));
        userListScrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
        userListScrollPane.getVerticalScrollBar().setPreferredSize(new Dimension(14, Integer.MAX_VALUE));
        userListScrollPane.setBorder(null);

        userList.setBackground(UserListCellRenderer.COLOR_BG);
        userList.setBorder(new EmptyBorder(5, 3, 5, 3));
        userList.setCellRenderer(new UserListCellRenderer());
        userList.addMouseListener(new MouseAdapter() {
            public void mousePressed(MouseEvent e) {
                if (showPopup && e.getButton() == MouseEvent.BUTTON3) {
                    int index = userList.locationToIndex(e.getPoint());
                    userList.setSelectedIndex(index);
                    
                    UserListEntry entry = (UserListEntry)userList.getSelectedValue();
                    if (entry != null) {
                        popupMenu.show(userList, e.getX(), e.getY());
                    }
                }
            }
        });
        
        infoPanel.setBackground(Color.GRAY);
        infoPanel.add(infoUserConnectedLabel);
        
        infoUserConnectedLabel.setFont(new Font("Arial", Font.PLAIN, 10));
        
        popupMenu.add(group1Item);
        popupMenu.add(group2Item);
        popupMenu.add(group3Item);
        popupMenu.add(group4Item);
        popupMenu.add(noGroupItem);
        popupMenu.addSeparator();
        popupMenu.add(makeParseSharer);
        popupMenu.add(removeParseSharer);
        
        
        group1Item.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent arg0) {
                UserListEntry entry = (UserListEntry)userList.getSelectedValue();
                if (entry != null && entry.userName.length() > 0) {
                    setGroupNumber(entry.userName, 1);
                }
            }
        });
        group2Item.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent arg0) {
                UserListEntry entry = (UserListEntry)userList.getSelectedValue();
                if (entry != null && entry.userName.length() > 0) {
                    setGroupNumber(entry.userName, 2);
                }
            }
        });
        group3Item.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent arg0) {
                UserListEntry entry = (UserListEntry)userList.getSelectedValue();
                if (entry != null && entry.userName.length() > 0) {
                    setGroupNumber(entry.userName, 3);
                }
            }
        });
        group4Item.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent arg0) {
                UserListEntry entry = (UserListEntry)userList.getSelectedValue();
                if (entry != null && entry.userName.length() > 0) {
                    setGroupNumber(entry.userName, 4);
                }
            }
        });
        noGroupItem.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent arg0) {
                UserListEntry entry = (UserListEntry)userList.getSelectedValue();
                if (entry != null && entry.userName.length() > 0) {
                    setGroupNumber(entry.userName, 0);
                }
            }
        });
        makeParseSharer.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent arg0) {
                UserListEntry entry = (UserListEntry)userList.getSelectedValue();
                if (entry != null && entry.userName.length() > 0) {
                    makeParseSharer(entry.userName);
                }
            }
        });
        removeParseSharer.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent arg0) {
                UserListEntry entry = (UserListEntry)userList.getSelectedValue();
                if (entry != null && entry.userName.length() > 0) {
                    removeParseSharer(entry.userName);
                }
            }
        });
    }
    
    public void setUserNames(String[] userNames) {
        userListModel.setListData(userNames);
    }
    
    public void setClientInfos(ClientInfo[] clientInfos) {
        userListModel.setListInfoData(clientInfos);
    }
    
    public void clearList() {
        userListModel.clear();
    }
    
    public void setShowPopup(boolean b) {
        showPopup = b;
    }
    
    public UserListModel getUserListModel() {
        return userListModel;
    }
    
    protected void setGroupNumber(String userName, int groupNumber) {
    }
    protected void makeParseSharer(String userName) {
    }
    protected void removeParseSharer(String userName) {
    }
}
